<?php
namespace Home\Controller;

class IndexController extends NavController {
    public function index(){
        //取出促销商品
        $goodsModel = D('Admin/Goods');
        $goods1 = $goodsModel->getPromoteGoods();
        $goods2 = $goodsModel->getRecGoods('is_hot');//热卖商品
        $goods3 = $goodsModel->getRecGoods('is_best');//推荐商品
        $goods4 = $goodsModel->getRecGoods('is_new');//新品
        $catModel = D('Admin/Category');
        $floorData = $catModel->floorData();
        $this->assign(array(
            'goods1' => $goods1,
            'goods2' => $goods2,
            'goods3' => $goods3,
            'goods4' => $goods4,
            'floorData' => $floorData,
        ));

        $this->assign(array(
            '_show_nav' => 1,
            '_page_title' => '首页',
            '_page_keywords' => '首页',
            '_page_description' => '首页',
        ));
        $this->display();
    }

    /****商品详情页****/
    public function goods(){

        //接收商品的ID
        $id = I('get.id');

        //根据ID取出商品信息
        $gModel = D('Admin/Goods');
        $info = $gModel->find($id);

        //导航
        $catModel = D('Admin/Category');
        $catPath = $catModel->parentPath($info['cat_id']);

        //取出商品相册
        $gpModel = D('Admin/goods_pic');
        $gpData = $gpModel->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        //取出商品的所有属性
        $gaModel = D('Admin/goods_attr');
        $gaData = $gaModel->alias('a')
            ->field('a.*,b.attr_name,b.attr_type')
            ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id=b.id')
            ->where(array(
                'a.goods_id' => array('eq',$id),
            ))->select();
        //把商品的唯一跟可选属性分开存放
        $uniAttr = array();
        $mulAttr = array();
        foreach($gaData as $k => $v){
            if($v['attr_type'] == '唯一'){
                $uniAttr[] = $v;
            } else {
                $mulAttr[$v['attr_name']][] = $v;
            }
        }
        $mpModel = D('Admin/member_price');
        $mpData = $mpModel->alias('a')
            ->field('a.price,b.level_name')
            ->join('LEFT JOIN __MEMBER_LEVEL__ b ON a.level_id=b.id')
            ->where(array(
                'a.goods_id' => array('eq',$id)
            ))->select();
        $viewPath = C('IMAGE_CONFIG');

        $goodsModel = D('Admin/Goods');
        $goods2 = $goodsModel->getRecGoods('is_hot');

        $this->assign(array(
            'goods2' => $goods2,
            'info' => $info,
            'catPath' => $catPath,
            'gpData' => $gpData,
            'uniAttr' => $uniAttr,
            'mulAttr' => $mulAttr,
            'mpData' => $mpData,
            'viewPath' => $viewPath['viewPath'],
        ));
        $this->assign(array(
            '_show_nav' => 0,
            '_page_title' => '商品详情页',
            '_page_keywords' => '商品详情页',
            '_page_description' => '商品详情页',
        ));
        $this->display();
    }


    /**处理浏览历史**/
    public function displayHistory(){
        $id = I('get.id', 0);
        //先从COOKIE中取出浏览历史的ID数组
        $data = isset($_COOKIE['display_history']) ? unserialize($_COOKIE['display_history']) : array();
        //把最新浏览的商品放在数组第一位
        array_unshift($data,$id);
        $data = array_unique($data);
        //只取6个
        if(count($data) > 6){
            $data = array_slice($data,0,6);
        }
        //数据存回COOKIE
        setcookie('display_history',serialize($data),time() + 30 * 86400,'/');
        $goodsModel = D('Admin/Goods');
        $data = implode(',',$data);
        $gData = $goodsModel->field('id,mid_logo,goods_name')->where(array(
            'id' => array('in',$data),
            'is_onsale' => array('eq',1),
        ))->order("FIELD('id',$data)")->select();
        echo json_encode($gData);
    }

    public function ajaxGetMemberPrice(){
        $goodsId = I('get.goods_id');
        $gModel = D('Admin/Goods');
        echo $gModel->getMemberPrice($goodsId);
    }
}